//
//  TabbarViewController.m
//  微博2期
//
//  Created by J on 15/11/17.
//
//

#import "TabbarViewController.h"
#import "HomeViewController.h"
#import "MessageCenterViewController.h"
#import "DiscoveryViewController.h"
#import "ProfileViewController.h"
#import "NavigationController.h"
#import "Tabbar.h"
@interface TabbarViewController ()<TabBarDelegate>

@end

@implementation TabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createViewcontrollers];
    
}

- (void)createViewcontrollers{
    NSArray * titles = @[@"首页",@"消息"];
    
    
    HomeViewController * home = [HomeViewController new];
    [self addChildVc:home title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
    
    
    
    MessageCenterViewController * messageCenter = [MessageCenterViewController new];
    [self addChildVc:messageCenter title:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
    
    DiscoveryViewController * discovery = [DiscoveryViewController new];
    [self addChildVc:discovery title:@"发现" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
    
    ProfileViewController * profile = [ProfileViewController new];
    [self addChildVc:profile title:@"我" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
    
//    NSLog(@"%@",self.tabBar);

    //更换系统自带tabbar
//    self.tabBar = [Tabbar new];
    Tabbar * tabbar = [Tabbar new];
//    tabbar.delegate = self;//tabbar自己声明的代理协议
    
    //相当于self.taBbar = tabBar;
    [self setValue:tabbar forKey:@"tabBar"];//相当于上面那一句 这句开始tabbar变成了TabbarViewController的属性了 里面的东西就不允许再被修改了这行代码过后tabbar.delegate控制器就是当前自己 就算不设置delegate
// reason: 'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'
    //不允许修改TabBar的degate属性 (这个TabBar是被TabbarViewController所管理的）
//    NSLog(@"%@",self.tabBar.delegate);
    
    
    
//    NSLog(@"%@ %@",self.childViewControllers,self.viewControllers);
    
//        tbc.viewControllers = @[vc1,vc2,vc3,vc4,vc5];
    //第二种方式添加
    
//    [self addChildViewController:home];
//    [self addChildViewController:messageCenter];
//    [self addChildViewController:discovery];
//    [self addChildViewController:profile];
//    
    //很多重复代码 将重复代码抽取到一个方法中
    //1.相同的代码放到一个方法中
    //2.不同的东西变成参数
    //3.在使用到这段代码的地方 调用方法 传递参数
  }


- (void)tabBarDidClickPlusButton:(Tabbar *)tabBar{
    
    UIViewController * vc =[UIViewController  new];
    vc.view.backgroundColor  = [UIColor orangeColor];
    [self presentViewController:vc animated:YES completion:nil];
    
    
}



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    JZJLog(@"%@",self.tabBar.subviews);

//    
//    int count = self.tabBar.subviews.count;
//    for (int i=0; i<count; i++) {
//        UIView * child = self.tabBar.subviews[i];
//        Class class = NSClassFromString(@"UITabBarButton");//这个类敲不出来 只能通过字符串转换
//        if ([child isKindOfClass:class]) {
////            NSLog(@"class");
//            
//            child.width = self.tabBar.width/count;
//            
//        }
//    }
//    
    

}
/**
 *  添加一个子控制器
 *
 *  @param childVC       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中图片
 */

- (void)addChildVc:(UIViewController*)childVC title:(NSString*)title image:(NSString*)image selectedImage:(NSString*)selectedImage {
    
    //设置子控制器的文字和图
    //    UIViewController *  =[[UIViewController alloc]init];
//    childVC.tabBarItem.title = title; // 设置tabbar文字
//    childVC.navigationItem.title = title; //设置navigationbar文字
    childVC.title = title ;//这句代码相当于前面两句 同时设置tabbar和navigationBar的文字

    
    //设置子控制器的图片
    childVC.tabBarItem.image = [UIImage imageNamed:image];//默认状态图片
    //声明:这张图片按照原始的样子显示出来,不要自动渲染成其他颜色
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    //文字样式
//    NSMutableDictionary * textAttrs = [NSMutableDictionary dictionary];
//    NSMutableDictionary * selecteTextAttrs = [NSMutableDictionary dictionary];
//    selecteTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
//    textAttrs[NSForegroundColorAttributeName] = Color(123, 123, 123);
    [childVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:Color(123, 123, 123)} forState:UIControlStateNormal];
    [childVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
//    childVC.view.backgroundColor = RamdomColor;
        //先给外面传进来的小控制器 包装一个导航控制器
    NavigationController * nav = [[NavigationController alloc]initWithRootViewController:childVC];
    [self addChildViewController:nav];
}



@end
